package br.com.fiap.beans.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import br.com.fiap.dao.impl.LoginDAOImpl;
import br.com.fiap.entity.Login;

@FacesValidator("br.com.fiap.beans.validator.LoginValidatorUsuario")
public class LoginValidatorUsuario implements Validator {
	
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

			LoginDAOImpl daoImpl = new LoginDAOImpl();
		
			Login login = daoImpl.findByName((String) value);
			
			if(login == null){
				FacesMessage msg = new FacesMessage("Usuario Invalido.");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
			}
	}

}
